package alfred.mdp.messages.client;

import alfred.mdp.Majordomo;
import alfred.mdp.messages.ProtocolMessage;
import org.zeromq.ZMsg;

import static alfred.mdp.utils.Utils.asString;

/**
 * Request <br>
 * Frame 0: Empty (zero bytes, invisible to REQ application)<br>
 * Frame 1: "MDPC01" (six bytes, representing MDP/Client v0.1)<br>
 * Frame 2: Service name (printable string)<br>
 * Frames 3+: Request body (opaque binary)<br>
 */
public class Request extends ProtocolMessage {

    public Request(String service, ZMsg message) {
        super(
                Majordomo.EMPTY,
                Majordomo.PROTOCOL_CLIENT_V,
                asZFrame(service)
        );
        getZMsg().addAll(message);
    }

    public Request(ZMsg received) {
        super(getSubMsg(received, 1));
        setClientId(get(received, 0));
    }

    public ZMsg getBody() {
        return getSubMsg(3);
    }

    public String getServiceName() {
        return asString(get(2).getData());
    }
}
